#!/bin/bash

ILC_SOFT=/afs/desy.de/project/ilcsoft/sw/i386_gcc41_sl5/v01-14-01
CALICE_SOFT=/afs/desy.de/group/flc/pool/shaojun/Calice_v04-06/build_x86_grid/calice_v04-06

export MARLIN_DLL=
export MARLIN_DLL=${CALICE_SOFT}/lib/libuserlib.so
export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/liblabview2lcio.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libraw2calohit.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libcellGeometry.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/librecoSiPM.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/librecoAnalysis.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libcalicesim.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libPrivate.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libhh_proc.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libRootTreeWriter.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libCALIB.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libCALIBMULTI.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libDeadAndNoisyTools.so
#export MARLIN_DLL=${MARLIN_DLL}:${CALICE_SOFT}/lib/libFITMIP.so


if [ $ADD_MARLIN_DLL ] ; then
    echo "***  Loading user libraries: $ADD_MARLIN_DLL"
    export MARLIN_DLL=$MARLIN_DLL:$ADD_MARLIN_DLL
fi

# test if libpdcap is already preloaded
echo $LD_PRELOAD | grep libpdcap > /dev/null
if [ $? -eq 1 ]; then
    for f in ./libpdcap.so /products/dcache/lib/libpdcap.so /opt/d-cache/dcap/lib/libpdcap.so ; do
	if [ -e $f ] ; then
	    export LD_PRELOAD=$LD_PRELOAD:$f
	    break
	fi
    done
fi

export DCACHE_CLIENT_ACTIVE=1

#if [ -e ${ILC_SOFT}/../lcio/v01-10-01-nda/lib/liblcio.so ] ; then
#    export LD_PRELOAD=${LD_PRELOAD}:${ILC_SOFT}/../lcio/v01-10-01-nda/lib/liblcio.so
#fi

export ROOTSYS=$ILC_SOFT/root/5.28.00f

#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${CALICE_SOFT}/../common/Marlin/lib:${CALICE_SOFT}/../common/lccd/lib
#exec $CALICE_SOFT/../common/Marlin/bin/Marlin $@

#this is necessary because of some gsidcap problem on the NAF:
MYSQL=`ls -1d ${ILC_SOFT}/mysql/*`
export LD_LIBRARY_PATH=:"${MYSQL}/lib/mysql:${LD_LIBRARY_PATH}"

#next line prevents empty cdb.log file
export COND_DB_DEBUGLOG=/dev/stdout

exec ${ILC_SOFT}/Marlin/*/bin/Marlin $@
